package com.example.bookkeepingelf.business.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.bookkeepingelf.R;
import com.example.bookkeepingelf.business.database.entity.AccountBook;
import com.example.bookkeepingelf.business.listener.OnOffListener;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;

public class NavigationActivity extends AppCompatActivity implements OnOffListener {

    private BottomNavigationView bottomNavigationView;

    private TextView textView;
    private Button button;

    private PopupWindow popupWindow;
    private ListView listView;

    private FloatingActionButton fabAdd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_navigation);
        this.fabAdd = findViewById(R.id.fab_add);

        this.replaceFrameLayout(R.id.frame_layout_title, new TitleFragment());
        this.replaceFrameLayout(R.id.frame_layout_content, new HomeContentFragment(this));

        this.fabAdd.setOnClickListener(v1 -> {
            Intent intent = new Intent(this, AccountingActivity.class);
            startActivity(intent);
        });




    }


    private void replaceFrameLayout(int fragmentLayoutId, Fragment fragment) {
        // 替换fragment
        //1、获取fragment管理器
        FragmentManager fragmentManager = getSupportFragmentManager();
        //2、开启事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        //3、替换fragment
        transaction.replace(fragmentLayoutId, fragment);
        //4、放入返回栈
        //5、提交事务
        transaction.commit();
    }

    /**
     * 隐藏添加按钮
     */
    private void hideAddButton() {
        // 动画：缩小并消失
        this.fabAdd.animate()
                .scaleX(0f) // 缩放到宽度为 0
                .scaleY(0f) // 缩放到高度为 0
                .alpha(0f)  // 透明度变为 0
                .setDuration(300) // 动画持续时间（毫秒）
                .withEndAction(new Runnable() {
                    @Override
                    public void run() {
                        // 动画结束后隐藏按钮
                        fabAdd.setVisibility(View.GONE);
                    }
                })
                .start(); // 开始动画

    }

    /**
     * 显示添加按钮
     */
    private void showAddButton() {
        // 还原动画
        fabAdd.setScaleX(0f);
        fabAdd.setScaleY(0f);
        fabAdd.setAlpha(0f);
        fabAdd.setVisibility(View.VISIBLE);

        fabAdd.animate()
                .scaleX(1f)
                .scaleY(1f)
                .alpha(1f)
                .setDuration(300)
                .start();
    }

    /**
     * 添加按钮的监听
     */
    @Override
    public void on() {
        this.showAddButton();
    }

    /**
     * 隐藏添加按钮
     */
    @Override
    public void off() {
        this.hideAddButton();
    }
}